<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/3
 * Time: 18:24
 */

namespace app\api\controller\v1;

use app\api\model\Goodscat as GoodscatModel;
use think\Exception;
use think\Log;
use think\Request;
use think\Session;

class Goodscat
{
    /**
     * 添加
     */
    public function add()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			
			if(empty($all['catname'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['type'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['isshow'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['orderby'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			
			$data = GoodscatModel::addOneGoodscat($all);
			
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**
     * 修改
     */
    public function update()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			
			if(empty($all['id'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['catname'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['type'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['isshow'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['orderby'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			
			$dataone = GoodscatModel::selectOneGoodscatByID($all['id']);
			if($dataone){
				
				$data = GoodscatModel::updOneGoodscat($all);
				if($data){
					return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
				}else{
					return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
				}
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
			
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**
     * 后台产品分类-列表
     */
    public function lists()
    {
		
		$data = GoodscatModel::GoodscatLists();
		if($data){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
		}
    }
	
	/**
     * 前台产品分类-列表
     */
    public function Homelists()
    {
		
		$data = GoodscatModel::GoodscatListsHome();
		return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		
    }
	
	/**
     * 删除一条
     */
    public function del($id)
    {
		if(empty($id)){
			return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
		}
		$dataone = GoodscatModel::selectOneGoodscatByID($id);
		if($dataone){
			$data = GoodscatModel::delOneGoodscat($id);
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!"]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
			}
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
    }
	
	/**查询一条记录*/
	public function find($id){
	
			
		$data = GoodscatModel::selectOneGoodscatByID($id);
		if($data){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
	}
	
	
}